﻿using UnityEngine;
using System.Collections;
using System;

public class CharacterViewer : MonoBehaviour {


	public enum EViewerTake
	{
		idle01,
		idle02,
		attack01,
		damage01,
		skill01,
		skill01_S,
		support_attack01,
		support_guard01,
		down01,
		downloop01,
		standup01,
		victory01,
		enterstage01,
		die01,
		forward01,
//		01_attack01_L,
//		01_attack01_M,
//		01_attack01_R,
//		01_enterstage01,
//		01_idle01,
//		01_idle02,
//		01_skill01,
//		01_skill02,
//		02_attack01_L,
//		02_attack01_M,
//		02_attack01_R,
//		02_enterstage01,
//		02_idle01,
//		02_idle02,
//		02_skill01,
//		02_skill02,
		Max ,
	}

	private static CharacterViewer instance;
	
	public static CharacterViewer Instance{
		get{
			return instance;
		}
	}	
	
	void Awake()
	{
		instance = this;
		playerPosParent = GameObject.Find("PCPos").transform.FindChild("4");
		enemyPosParent = GameObject.Find("NPCPos").transform.FindChild("4");

		GameObject uiExist = GameObject.Find("UI Root");
		if(uiExist!=null)
			uiExist.SetActive(false);

		if( GameObject.Find("ViewerUI") == null)
		{
			GameObject viewerUI = Instantiate( Resources.Load ("Prefab/Battle/ViewerUI") as GameObject ) as GameObject;
		}
//		OriginalCamTrans = new Transform[3];
//		OriginalCamTrans[0] = GameObject.Find("CamPos1").transform;
//		OriginalCamTrans[1] = GameObject.Find("CamPos2").transform;
//		OriginalCamTrans[2] = GameObject.Find("CamPos3").transform;

		GameObject go = Instantiate( Resources.Load ("Prefab/Battle/BattleResManager") as GameObject ) as GameObject;
	}

	void Start()
	{
		InitCam ();
		if(playerPrefab==null)
			playerPrefab = Resources.Load ("Prefab/Battle/Character/Assassin_M_BrietraLS_A1") as GameObject;
		if(enemyPrefab==null)
			enemyPrefab = Resources.Load ("Prefab/Battle/Character/Haskin_M_C1") as GameObject;
		if(playerPrefab!=null)
			CreatePlayer();
		if(enemyPrefab!=null)
			CreateEnemy();
		playerPrefabOld = playerPrefab;
		enemyPrefabOld = enemyPrefab;
	}

	Transform playerPosParent;
	Transform enemyPosParent;
	public GameObject playerPrefab;
	GameObject playerPrefabOld;
	public GameObject enemyPrefab;
	GameObject enemyPrefabOld;
	[HideInInspector]
	public EViewerTake takeToPlay;

	GameObject playerGo;
	GameObject enemyGo;
	CharViewerPawn player;
	CharViewerPawn enemy;

	[HideInInspector]
	public GameObject cameraRoot;
	[HideInInspector]
	public LegionTouchCam touchCam;
	[HideInInspector]
	public GameObject middleCam;

	public Transform[] OriginalCamTrans = new Transform[3];
	[HideInInspector]
	public float OriginalCamFOV;

	public bool turnOffSkillCam;
	public bool turnOffCamShake;
	public bool repeatPlay;

	[HideInInspector]
	public float curTimeScale = 1;

	void InitCam()
	{
		if(cameraRoot==null) {
			OriginalCamFOV = Camera.main.fieldOfView;
			cameraRoot = CamTakeSetting.Instance.cameraRoot;
			CamTakeSetting.Instance.allowTouchCam=true;
			if(BattleManager.Instance!=null && BattleManager.Instance.cameraRoot !=null) {
				cameraRoot = BattleManager.Instance.cameraRoot;
				CamTakeSetting.Instance.cameraRoot = cameraRoot;
			}
			touchCam = cameraRoot.GetComponent<LegionTouchCam>();
			if(touchCam==null) {
				touchCam = cameraRoot.AddComponent<LegionTouchCam>();
				touchCam.Reset();
			}
			middleCam = cameraRoot.transform.FindChild("Middle").gameObject;
			
			EventlineStaticActor.Camera = cameraRoot;
			if(middleCam!=null)
				EventlineStaticActor.MiddleCam = middleCam;
			EventlineStaticActor.MainCamera = Camera.main.gameObject;

			if(cameraRoot.GetComponent<LegionTouchCamKeys>() == null)
				cameraRoot.AddComponent<LegionTouchCamKeys>().Reset();
			if(cameraRoot.GetComponent<LegionTouchCamMouse>() == null)
				cameraRoot.AddComponent<LegionTouchCamMouse>().Reset();

			GameObject originalCamPosGo = new GameObject();
			originalCamPosGo.name="originalCamPosGo";
			originalCamPosGo.transform.position = cameraRoot.transform.position;
			originalCamPosGo.transform.rotation = cameraRoot.transform.rotation;
			OriginalCamTrans[0] = originalCamPosGo.transform;
		}
	}

	void CreatePlayer()
	{
		if(playerGo!=null)
			Destroy(playerGo);
		playerGo = Instantiate(playerPrefab) as GameObject;
		playerGo.name = playerPrefab.name;
		playerGo.transform.parent = playerPosParent;
		playerGo.transform.localPosition = Vector3.zero;
		playerGo.transform.localRotation = Quaternion.identity;
		playerGo.transform.localScale = Vector3.one;
		playerGo.transform.parent = null;
		playerGo.GetComponent<Animation>().playAutomatically=false;
		player = playerGo.AddComponent<CharViewerPawn>();
		player.Init(true);
	}
	void CreateEnemy()
	{
		if(enemyGo!=null)
			Destroy(enemyGo);
		enemyGo = Instantiate(enemyPrefab) as GameObject;
		enemyGo.name = enemyPrefab.name;
		enemyGo.transform.parent = enemyPosParent;
		enemyGo.transform.localPosition = Vector3.zero;
		enemyGo.transform.localRotation = Quaternion.identity;
		enemyGo.transform.localScale = Vector3.one;
		enemyGo.transform.parent = null;
		enemyGo.GetComponent<Animation>().playAutomatically=false;
		enemy = enemyGo.AddComponent<CharViewerPawn>();
		enemy.Init(false);
	}


	public void OnAction(int nType)
	{
		if(nType>=1 && nType<=3)
			ResetCamera(nType);
		else if(nType==0)
			PlayTake();
		else if(nType==10)
			ToggleTimeScale();
	}

	void ToggleTimeScale()
	{
		if(curTimeScale==1)
			curTimeScale=0.5f;
		else if(curTimeScale==0.5)
			curTimeScale=0.25f;
		else if(curTimeScale==0.25)
			curTimeScale=1;
		Time.timeScale=curTimeScale;
	}

	public void ResetCamera(int nType)
	{
		//CamTakeSetting.Instance.SetBackTakeCamWaitTime(0);
		touchCam.DeactivateFreeCam();
		BackToOriginalCamPos(nType);
	}

	public void BackToOriginalCamPos(int nType)
	{
		Debug.Log("BackToOriginalCamPos  curfov = "+Camera.main.fieldOfView+"   curTimeScale="+Time.timeScale);
		Camera.main.fieldOfView = OriginalCamFOV;
		Time.timeScale=1.0f;
		if(OriginalCamTrans!=null && nType-1 < OriginalCamTrans.Length && OriginalCamTrans[nType-1] !=null) {
			cameraRoot.transform.position = OriginalCamTrans[nType-1].position;
			cameraRoot.transform.rotation = OriginalCamTrans[nType-1].rotation;
		}
	}

	public void SetTake(string _takeName)
	{
		foreach (EViewerTake etake in Enum.GetValues(typeof(EViewerTake)))
		{
			if(_takeName== etake.ToString()) {
				takeToPlay = etake;
				break;
			}
		}
	}

	public void PlayTake()
	{
		if(player!=null)
			player.PlayTake(takeToPlay, enemyGo, enemy);
	}

	void Update()
	{
		if(playerPrefabOld!=playerPrefab) {
			playerPrefabOld = playerPrefab;
			CreatePlayer();
		}
		if(enemyPrefabOld!=enemyPrefab) {
			enemyPrefabOld = enemyPrefab;
			CreateEnemy();
		}

		if(turnOffSkillCam) {
			EventlineStaticActor.Camera=null;
		}
		else {
			if(EventlineStaticActor.Camera==null)
				EventlineStaticActor.Camera=cameraRoot;
		}

		if(turnOffCamShake) {
			EventlineStaticActor.MiddleCam=null;
		}
		else {
			if(EventlineStaticActor.MiddleCam==null)
				EventlineStaticActor.MiddleCam = middleCam;
		}
	}
}
